# Maintainer: Alexey Pavlov <Alexpux@gmail.com>

pkgbase=ncurses
pkgname=('ncurses' 'ncurses-devel')
_basever=6.0
_date=20151121
pkgver=${_basever}.${_date}
pkgrel=1
pkgdesc="System V Release 4.0 curses emulation library"
arch=('i686' 'x86_64')
url="http://www.gnu.org/software/ncurses/"
license=('MIT')
source=("${pkgbase}-${pkgver}.tar.gz"::"http://invisible-island.net/datafiles/current/${pkgbase}.tar.gz"
        0001-CRITICAL-fix-return-value-of-drv_CanHandle-on-unknow.patch)
md5sums=('3b4a9373d851e138aff8e60d7f5d01ed'
         'd028c8a447339bf5f1762b91938e66d6')

prepare() {
  cd ${srcdir}/${pkgbase}-${_basever}-${_date}

  #patch -p1 -i ${srcdir}/0001-CRITICAL-fix-return-value-of-drv_CanHandle-on-unknow.patch
}

build() {
  cd ${srcdir}/${pkgbase}-${_basever}-${_date}

  ./configure \
    --build=${CHOST} \
    --with-install-prefix=/usr \
    --without-ada \
    --with-shared \
    --with-cxx-shared \
    --with-normal \
    --without-debug \
    --disable-relink \
    --disable-rpath \
    --with-ticlib \
    --without-termlib \
    --enable-widec \
    --enable-ext-colors \
    --enable-ext-mouse \
    --enable-sp-funcs \
    --with-wrap-prefix=ncwrap_ \
    --enable-sigwinch \
    --enable-term-driver \
    --enable-colorfgdg \
    --enable-tcap-names \
    --disable-termcap \
    --disable-mixed-case \
    --with-pkg-config \
    --enable-pc-files \
    --with-manpage-format=normal \
    --with-manpage-aliases \
    --with-default-terminfo-dir=/usr/share/terminfo \
    --enable-echo \
    --mandir=/usr/share/man \
    --includedir=/usr/include/ncursesw \
    --with-build-cflags=-D_XOPEN_SOURCE_EXTENDED \
    --with-pkg-config-libdir=/usr/lib/pkgconfig

  make
  make DESTDIR=${srcdir}/dest install
}

package_ncurses() {
  depends=('msys2-runtime' 'gcc-libs')
  groups=('base')

  mkdir -p ${pkgdir}/usr
  cp -rf $srcdir/dest/usr/bin ${pkgdir}/usr/
  rm -f ${pkgdir}/usr/bin/*-config
  mkdir -p ${pkgdir}/usr/lib
  cp -rf $srcdir/dest/usr/lib/terminfo ${pkgdir}/usr/lib/
  cp -rf $srcdir/dest/usr/share ${pkgdir}/usr/

  # install license, rip it from the readme
  cd ${srcdir}/$pkgname-${_basever}-${_date}
  install -dm755 ${pkgdir}/usr/share/licenses/$pkgname
  grep -B 100 '$Id' README > ${pkgdir}/usr/share/licenses/${pkgname}/license.txt
}

package_ncurses-devel() {
  groups=('development')
  options=('staticlibs')
  depends=("ncurses=$pkgver")
  pkgdesc="NCURSES headers and libraries"

  mkdir -p ${pkgdir}/usr/bin
  cp -f $srcdir/dest/usr/bin/*-config ${pkgdir}/usr/bin/
  cp -rf $srcdir/dest/usr/include ${pkgdir}/usr/
  cp -rf $srcdir/dest/usr/lib ${pkgdir}/usr/
  rm -rf ${pkgdir}/usr/lib/terminfo

  cp -rf ${pkgdir}/usr/include/ncursesw ${pkgdir}/usr/include/ncurses

  cp -f ${pkgdir}/usr/lib/libncursesw.a ${pkgdir}/usr/lib/libncurses.a
  cp -f ${pkgdir}/usr/lib/libncursesw.dll.a ${pkgdir}/usr/lib/libncurses.dll.a

  cp -f ${pkgdir}/usr/lib/libpanelw.a ${pkgdir}/usr/lib/libpanel.a
  cp -f ${pkgdir}/usr/lib/libpanelw.dll.a ${pkgdir}/usr/lib/libpanel.dll.a

  cp -f ${pkgdir}/usr/lib/libmenuw.a ${pkgdir}/usr/lib/libmenu.a
  cp -f ${pkgdir}/usr/lib/libmenuw.dll.a ${pkgdir}/usr/lib/libmenu.dll.a

  cp -f ${pkgdir}/usr/lib/pkgconfig/formw.pc ${pkgdir}/usr/lib/pkgconfig/form.pc
  cp -f ${pkgdir}/usr/lib/pkgconfig/menuw.pc ${pkgdir}/usr/lib/pkgconfig/menu.pc
  cp -f ${pkgdir}/usr/lib/pkgconfig/ncurses++w.pc ${pkgdir}/usr/lib/pkgconfig/ncurses++.pc
  cp -f ${pkgdir}/usr/lib/pkgconfig/ncursesw.pc ${pkgdir}/usr/lib/pkgconfig/ncurses.pc
  cp -f ${pkgdir}/usr/lib/pkgconfig/panelw.pc ${pkgdir}/usr/lib/pkgconfig/panel.pc
  cp -f ${pkgdir}/usr/lib/pkgconfig/ticw.pc ${pkgdir}/usr/lib/pkgconfig/tic.pc
}
